package com.homedo.odin.solution.controller.mng;

import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.contract.mng.response.column.ColumnSkuListResponseBean;
import com.homedo.odin.solution.enumcode.basic.DeleteEnum;
import com.homedo.odin.solution.model.request.mng.column.AddColumnRelevanceSkuRequestBean;
import com.homedo.odin.solution.model.request.mng.column.RemoveColumnRelevanceSkuRequestBean;
import com.homedo.odin.solution.service.ColumnSkuServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: ZhangLei
 * @date: 2018/4/9
 * @Description:
 */
@Api(description = "栏目", tags = {"Column"},
        produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/mng/columns")
public class MngColumnSkuController {

    @Autowired
    private ColumnSkuServiceImpl columnSkuService;

    @PostMapping("/skus")
    @ApiOperation(value = "OMS_新增栏目关联sku", httpMethod = "POST")
    public ResultData batchAddColumnSku(
            @Valid @RequestBody AddColumnRelevanceSkuRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        columnSkuService.batchAddColumnSku(requestBean);
        return ResultData.newInstance(Meta.CODE_200, null);
    }

    @DeleteMapping("/skus")
    @ApiOperation(value = "OMS_移除栏目关联sku", httpMethod = "DELETE")
    public ResultData delColumnSku(
            @Valid @RequestBody RemoveColumnRelevanceSkuRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        columnSkuService.delColumnSku(requestBean);
        return ResultData.newInstance(Meta.CODE_200, null);
    }


    @GetMapping("/{columnId}/skus")
    @ApiOperation(value = "OMS_根据栏目id查询sku列表", httpMethod = "GET", response = ColumnSkuListResponseBean.class)
    public ResultData getSkuListBycolumnId(@ApiParam(value = "栏目columnId", required = true)
    @PathVariable(value = "columnId") Long columnId) {
        if (!NumberHelper.validate(columnId)) {
            throw new BizException(Meta.CODE_451.getCode(), "columnId参数非法");
        }
        return ResultData.newInstance(Meta.CODE_200,
                columnSkuService.getSkuListByColumnId(columnId, DeleteEnum.NOT_DEL.getDbValue()));
    }

}
